package cn.edu.jxau.test;

import java.util.Objects;
import java.util.concurrent.ExecutionException;

import cn.edu.jxau.util.LinkedStack;
import cn.edu.jxau.util.Stack;

public class Main {

	public static void main(String[] args) throws InterruptedException, ExecutionException {
		
		MinStack stack = new MinStack();
		stack.push(1);
		System.out.println(stack.min());
		stack.push(2);
		System.out.println(stack.min());
		stack.push(0);
		System.out.println(stack.min());
		stack.push(3);
		System.out.println(stack.min());
		stack.push(1);
		System.out.println(stack.min());
	}
}

class MinStack {

	private Stack<Integer> stack;
	private Stack<Integer> auxStack;

	public MinStack() {
		stack = new LinkedStack<>();
		auxStack = new LinkedStack<>();
	}

	public void push(Integer i) {

		Objects.requireNonNull(i, "参数i不能为null");
		stack.push(i);
		Integer min = min();
		if (min == null || i < min) {
			auxStack.push(i);
		} else {
			auxStack.push(min);
		}
	}

	public Integer pop() {

		if (stack.isEmpty()) {
			return null;
		}
		auxStack.pop();
		return stack.pop();
	}

	public Integer min() {
		return auxStack.peek();
	}
}